package grimbo.goodwinfooty.processing;

import grimbo.goodwinfooty.FootyMatrixToJS;
import grimbo.goodwinfooty.service.model.FootyMatrix;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class FootyMatrixToJSProcessor implements Processor {
	public final static String CTX_FOOTY_MATRIX = "footyMatrix";
	public final static String CTX_JS_FILENAME = "jsFilename";

	public String process(Map context) throws Exception {
		FootyMatrix footy = (FootyMatrix) context.get(CTX_FOOTY_MATRIX);
		String filename = (String) context.get(CTX_JS_FILENAME);
		convert(footy, filename);
		return null;
	}

	private void convert(FootyMatrix footy, String filename) throws IOException {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filename);
			FootyMatrixToJS toJS = new FootyMatrixToJS(footy, fos);
			toJS.convert();
		} finally {
			if (null != fos) {
				fos.close();
			}
		}
	}
}
